An example of testing the HelloWorld Application using give below. This application uses the junit framework to test the application.
index.jsp
<%@ taglib prefix="s" uri="/struts-tags"%> <head> <title>Annotation Example</title> <style type="text/css"> @import url(css/main.css); </style> <style> .errorMessage { color: green; } </style> <body bgcolor="#A3A3FF"> <center> <h2>Please Enter Your Name</h2> <s:form action="hello"> <s:textfield name="userName" label="Please Specy Your Name" /> <s:submit /> </s:form></center> </body>
successPage.jspADS_TO_REPLACE_1
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Annotation Example</title> </head> <body bgcolor="#9E9EE8"> <center> <h1>${message}</h1> </center> </body> </html>
HelloWorldAction.java
package net.roseindia.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; public class HelloWorldAction { private static final long serialVersionUID = 1L; String userName = ""; String message; @Action(value = "/hello", results = { @Result(name = "error", location = "/jsp/index.jsp"), @Result(name = "success", location = "/jsp/successPage.jsp") }) public String execute() throws Exception { if (getUserName().equals("")) { return "error"; } else { message = "Hello " + userName; return "success"; } } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
HelloWorldActionTest.java
package net.roseindia.test; import net.roseindia.action.HelloWorldAction; import junit.framework.TestCase; public class HelloWorldActionTest extends TestCase { String result; String classResult; HelloWorldAction testObject = new HelloWorldAction(); @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); result = "success"; try { testObject.setUserName("Alex Ferrioda"); classResult = testObject.execute(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testExecute() { assertTrue(result.equalsIgnoreCase(classResult)); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); } }
Download Select Source CodeADS_TO_REPLACE_2
Advertisements
Ads
Ads